﻿using SangaiTech.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SangaiTech.Mvc.Areas.api.Controllers
{
    public class EmployeeController : ApiController
    {
        private readonly IEmployeeRepository _employeeRepository;
        public EmployeeController(IEmployeeRepository employeeRepository)
        {
            _employeeRepository = employeeRepository;
        }

        // GET api/<controller>
        //[System.Web.Http.Route("api/employee/values")]
        public IHttpActionResult Get()
        {
            var employees = _employeeRepository.All().Where(e => e.IsRetired==null || e.IsRetired == false).ToList();
            return Ok(employees);
        }

        // GET api/<controller>/5
        public Employee Get(int id)
        {
            return _employeeRepository.All().Where(e => e.EmployeeId == id).FirstOrDefault();
        }

        // POST api/<controller>
        public IHttpActionResult Post([FromBody]Employee employee)
        {
            var isMatch = System.Text.RegularExpressions.Regex.IsMatch(employee.FirstName, "^[a-zA-Z0-9_@.-]+$");
            if (!isMatch)
            {
                return BadRequest("Special characters are not allowed in FirstName");
            }
            else if(!employee.FirstName.StartsWith("X"))
            {
                return BadRequest("FirstName should always start with X");
            }
            else
            {
                //TODO: insert
                return Ok();
            }
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]Employee employee)
        {
            throw new NotImplementedException();
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            throw new NotImplementedException();
        }
    }
}